package com.example.sunnyweather.ui.place

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.lifecycle.livedata.ktx.R
import androidx.recyclerview.widget.RecyclerView
import com.example.sunnyweather.databinding.PlaceItemBinding
import com.example.sunnyweather.logic.model.Place

class PlaceAdapter(private val fragment:Fragment , private val placeList:List<Place>) : RecyclerView.Adapter<PlaceAdapter.ViewHolder>(){

    private lateinit var binding : PlaceItemBinding
    inner class ViewHolder(view:View) : RecyclerView.ViewHolder(view){
        val placeName : TextView =binding.placeName
//            view.findViewById(R.id.placeName)
        val placeAddress : TextView = binding.placeAddress
//            view.findViewById(R.id.placeAddress)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
//        val view = LayoutInflater.from(parent.context).inflate(androidx.core.R.layout.place_item,parent,false)
        val binding = PlaceItemBinding.inflate(LayoutInflater.from(parent.context),parent,false)
        return ViewHolder(binding.root)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val place = placeList[position]
        holder.placeName.text = place.name
        holder.placeAddress.text = place.address
    }

    override fun getItemCount(): Int {
        return placeList.size
    }

}